package com.simpleplayer.views.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.os.Handler;

//自定义按钮，按压后短时间内会置灰，无法响应点击事件
@RemoteViews.RemoteView
public class CustomButton extends androidx.appcompat.widget.AppCompatButton {

    private static final String TAG = "CustomButton";
    private static final long UNFREEZE_TIME_OUT = 1000; // 解冻超时时间，单位(ms)

    private final Context mContext;
    private OnClickListenerAdapter mListenerAdapter = null;

    public CustomButton(@NonNull Context context) {
        this(context, null);
    }

    public CustomButton(@NonNull Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public CustomButton(@NonNull Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        if (mListenerAdapter == null) {
            mListenerAdapter = new OnClickListenerAdapter(l, UNFREEZE_TIME_OUT);
        } else {
            if (mListenerAdapter.getBaseListener() != l) {
                mListenerAdapter.unfreeze();
                mListenerAdapter = new OnClickListenerAdapter(l, UNFREEZE_TIME_OUT);
            }
        }
        super.setOnClickListener(mListenerAdapter);
    }

    public void unfreeze() {
        if (mListenerAdapter != null) {
            mListenerAdapter.unfreeze();
        }
    }

    private class OnClickListenerAdapter implements OnClickListener {

        private static final int MAX_CLICK_COUNT = 3; // 冻结后弹出提示的点击次数

        private final CustomDialog tipDialog; // 点击过快提示弹窗
        private final OnClickListener listener;
        private final Handler handler;
        private final long timeOut; // 冻结的超时时间，冻结后 timeOut(ms) 后会自动解冻
        private int count = 0;   // 冻结时点击次数，解冻时清零
        private boolean isActivated = true;

        public OnClickListenerAdapter(OnClickListener l, long timeOut) {
            this.listener = l;
            this.timeOut = timeOut;
            this.handler = new Handler();
            this.tipDialog = new CustomDialog(CustomButton.this.mContext);
            tipDialog.setTip("点击频率过快，请等待" + timeOut / 1000 + "s后再次尝试");
        }

        @Override
        public void onClick(View v) {
            if (isActivated) {
                freeze();
                listener.onClick(v);
            } else {
                count++;
                if (count >= MAX_CLICK_COUNT) {
                    tipDialog.show();
                }
            }
        }

        public OnClickListener getBaseListener() {
            return listener;
        }

        private class UnfreezeRunnable implements Runnable {
            @Override
            public void run() {
                unfreeze();
            }
        }
        private Runnable unfreezeRunnable = null;

        // 冻结监听器，不再接收onClick事件
        private void freeze() {
            isActivated = false;
            unfreezeRunnable = new UnfreezeRunnable();
            handler.postDelayed(unfreezeRunnable, timeOut);
        }

        // 解冻监听器，可继续接收onClick事件
        public void unfreeze() {
            isActivated = true;
            count = 0;
            if (unfreezeRunnable != null) {
                handler.removeCallbacks(unfreezeRunnable);
                unfreezeRunnable = null;
            }
        }
    }
}
